using System;
using System.Windows.Forms;

using Office = Microsoft.Office.Core;
using Outlook = Microsoft.Office.Interop.Outlook;
using Microsoft.Office.Interop.Outlook;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.IO;
using Ionic.Zip;


namespace MAPISampleCSharp
{
    public partial class MAPISampleCSharp : Form
    {
        public MAPISampleCSharp()
        {
            InitializeComponent();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            object missing = System.Reflection.Missing.Value;
            string bodyText = string.Empty;
            string subject = string.Empty;
            // get the Outlook Application Object
            Outlook.Application outlookApplication = new Outlook.Application();

            // get the namespace object
            Outlook.NameSpace olNamespace = outlookApplication.GetNamespace("MAPI");

            // Logon to Session, here we use an already opened Outlook
           // olNamespace.Logon(missiing, missing, true, true);

            olNamespace.Logon("elookinto", "elookinto", false, true);
            // create a new email and fill it with the infos provided in the Form
            // Outlook.MailItem newMail = outlookApplication.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
            //MAPIFolder fs = olNamespace.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
            //MailItem newMail = (MailItem)fs.Items[0];
            // here we use our C++ library
            MAPIConcubine.Fabric fabric = new MAPIConcubine.Fabric();
            
            Outlook.MAPIFolder gmailFolder = olNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            //gmailFolder = olNamespace.Folders["elookinto@127.0.0.1"];
           // gmailFolder.Folders.

            //Outlook.MAPIFolder temp = null;

            //ExtractItems(null, gmailFolder, fabric);
            
            try
            {
              
                //temp = gmailFolder.Folders["mytemp"]
                String pst = @"c:\temp\my4.pst";
                olNamespace.AddStore(pst);
                Store addedStore = null;
                foreach (Store store in olNamespace.Stores)
                {
                    if (store.FilePath != null && store.FilePath.Equals(pst))
                    {
                        addedStore = store;
                        break;
                    }
                }

                //olNamespace.Stores.
                //MessageBox.Show(olNamespace.Stores["my2"].FilePath);
                Outlook.MAPIFolder pstFolder = addedStore.GetRootFolder();
                ExtractItems(null, pstFolder, fabric);

                olNamespace.RemoveStore(pstFolder);
                pstFolder = null;


               // MessageBox.Show("abc");
            }
            catch (System.Runtime.InteropServices.COMException ee)
            {
                MessageBox.Show(ee.ToString());
            }
            // that's all
            fabric = null;
            // logoff from namespace (session)
            olNamespace.Logoff();

            //release reference o outlook object
            outlookApplication = null;

            // Let the Garbagecollector do his work 
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }


        private void ExtractItems(List<MailItem> mailItems, MAPIFolder folder, MAPIConcubine.Fabric fabric)
        {
            Items items = folder.Items;

            int itemcount = items.Count;

            foreach (object item in items)
            {
                if (item is MailItem)
                {
                    MailItem mailItem = item as MailItem;

                    Outlook.MailItem someMail = item as Outlook.MailItem;
                    if (someMail != null)
                    {

                        String allHeaders = fabric.ReadMessageBody(someMail.MAPIOBJECT);
                        Dictionary<String, Object> hash = fabric.ReadSecureData(someMail.MAPIOBJECT);
                        MessageBox.Show(this, String.Format("The smtp headers for the message are: {0}, {1}, {2} ",
                            hash["sender"], hash["to"], someMail.ConversationID));
                        String tempDir = Path.GetTempPath();
                        DirectoryInfo path = Directory.CreateDirectory(Path.Combine(tempDir, someMail.ConversationID));
                       
                       // MessageBox.Show(ss);
                        for (int i = 1; i <= someMail.Attachments.Count; i++)
                        {
                            String fileName = Path.Combine(path.FullName, someMail.Attachments[i].FileName);
                            someMail.Attachments[i].SaveAsFile(fileName);
                        }

                        ZipFile zf = new ZipFile(@"c:\temp\" + someMail.ConversationID +".zip");
                       
                        zf.AddDirectory(path.FullName);
                        zf.Save();
                        path.Delete(true);
                        someMail = null;
                    }
                    //mailItems.Add(mailItem);
                }
            
            }

            foreach (Folder subfolder in folder.Folders)
            {
                ExtractItems(null, subfolder, fabric);
            } 

        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }



    }
}